package com.chen.shopping_order_customer_api.controller;

import com.chen.shopping_common.pojo.CartGoods;
import com.chen.shopping_common.pojo.Orders;
import com.chen.shopping_common.result.BaseResult;
import com.chen.shopping_common.service.CartService;
import com.chen.shopping_common.service.OrderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/orders")
public class OrderController {

    @DubboReference
    private OrderService orderService;
    @DubboReference
    private CartService cartService;

    //生成订单
    @PostMapping("/add")
    public BaseResult<Orders> add(@RequestHeader Long userId , @RequestBody Orders orders){
        //保存订单
        orders.setUserId(userId);
        Orders order = orderService.add(orders);
        //从购物车redis中删除数据
        List<CartGoods> cartGoods = orders.getCartGoods();
        for (CartGoods cartGood : cartGoods) {
            cartService.deleteCart(userId,cartGood.getGoodId());
        }
        return BaseResult.ok(order);
    }


    //查询订单详情
    @GetMapping("/findById")
    public BaseResult<Orders> findById(String id){
        Orders orders = orderService.findById(id);
        return BaseResult.ok(orders);
    }

    //查询用户订单
    @GetMapping("/findUserOrders")
    public BaseResult<List<Orders>> findByUser(@RequestHeader Long userId ,Integer status){
        List<Orders> byUser = orderService.findByUser(userId, status);
        return BaseResult.ok(byUser);
    }


}
